package cn.qqcn.common;

import cn.qqcn.emp.entity.Dept;
import cn.qqcn.emp.entity.Emp;
import cn.qqcn.emp.service.EmpService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CommonController {

    @Autowired
    private EmpService empService;

    //首页
    @GetMapping("/")
    public String index(){
        return "index";
    }

    //登录
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    //验证码
    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response)throws Exception{
        CaptchaUtil.out(request,response);
    }

    //首页统计
    @GetMapping("/welcome")
    public String welcome(){ return "welcome"; }

    //用户管理 普通用户
    @GetMapping("/emp/regular")
    public String toEmpListUI(){
        return "emp/regularuser";
    }

    //用户管理 新增普通用户
    @GetMapping("/emp/addreg")
    public String addregUI(Model model){
        List<Dept> deptList = empService.getAllDept();
        model.addAttribute("deptlist",deptList);
        return "emp/addreg";
    }

    //用户管理 编辑普通用户
    @GetMapping("/emp/{id}")
    public String getEmpById(@PathVariable("id") Integer id, Model model) {
        Emp emp = empService.getEmpById(id);
        model.addAttribute("emp",emp);
        model.addAttribute("deptlist",empService.getAllDept());
        return "emp/empEdit";
    }

    //用户管理 管理用户
    @GetMapping("/emp/opsdf")
    public String opsdf(){
        return "emp/glyu";
    }

    //注销
    @GetMapping("/cancellation")
    public String cancellation(HttpSession session){
        // 删除会话中的特定属性
        session.removeAttribute("userInfo");
        return "session";
    }

    @GetMapping("/404")
    public String notFound(){
        return "errs/404";
    }
}
